package com.boruiimage.demo.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boruiimage.demo.doMain.*;
import com.boruiimage.demo.mapperDao.filePart;
import com.boruiimage.demo.mapperDao.registerMapperDao;
import com.boruiimage.demo.repository.imgInfoDao;
import com.boruiimage.demo.repository.registerMemberUserDao;
import com.boruiimage.demo.utils.imgCard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class registerMemberDaoImpl implements registerMemberUserDao {
    Result rs=new Result();
    @Autowired
    registerMapperDao rmb;
    //如果不是会员直接添加会员
    @Override
    public Result addMemberUser(String userName, Integer systemCount, Integer personCount, Integer memberCode) {
        int i=rmb.addMemberUser(userName,systemCount,personCount,memberCode);
        if(i>0){
            rs.setCode(1);
        }else {
            rs.setCode(0);
        }
        return rs;
    }
    //添加记录
    @Override
    public Result addRechargeInfo(String userName, String payContext, Double payNum, String rechargeContext) {
        int add=rmb.addRechargeInfo(userName,payContext,payNum,rechargeContext);
        if(add>0){
            rs.setCode(1);
        }else {
            rs.setCode(0);
        }
        return rs;
    }
    //如果是会员修改累加次数并且更新会员开通时间
    @Override
    public Result upMemberUser(Integer systemCount, Integer personCount, Integer memberCode, String userName) {
        int i=rmb.upMemberUser(systemCount,personCount,memberCode,userName);
        int s=rmb.updateMemberUserData(userName);
        if(i>0&&s>0){
            rs.setCode(1);
        }else
        {
            rs.setCode(0);
        }
        return rs;
    }
    //查询该用户是否是会员
    @Override
    public memberuser us(String userName) {
        memberuser us = rmb.us(userName);
        return us;
    }
   // 开通会员时通过vipcode来获取会费
    @Override
    public member moneyByVipCode(Integer vipCode) {
        return rmb.moneyByVipCode(vipCode);
    }
    //一次性服务通过产品code获取金额
    @Override
    public Integer moneyByServerCode(Integer serverCode) {
        return rmb.moneyByServerCode(serverCode);
    }
    //通过vipcode获取人工次数和系统次数
    @Override
    public member mb(Integer vipCode) {
        return rmb.mb(vipCode);
    }
    //查询用户信息
    @Override
    public List<memberuser> listMemberuser(String userName) {
        return rmb.listMemberuser(userName);
    }
   // 查询会员充值记录
    @Override
    public List<recharacterinfo> selByrecharacterinfo(String userName) {
        return rmb.selByrecharacterinfo(userName);
    }
    //查询会员充值记录数量
    @Override
    public int selByrecharacterinfoCount(String userName) {
        return rmb.selByrecharacterinfoCount(userName);
    }

    //修改开通时间
    @Override
    public int upmemberuserData(String userName, Integer memberCode) {
        return rmb.upmemberuserData(userName,memberCode);
    }
    //查询该会员的系统数量是否大于0
    @Override
    public memberuser meSleect(String userName) {
        return rmb.meSleect(userName);
    }
    //查询该会员的人工数量是否大于0
    @Override
    public memberuser meSleectPerson(String userName) {
        return rmb.meSleectPerson(userName);
    }
    //通过产品code获取产品服务类型
    @Override
    public serverInfo selectServerInfo(Integer serverCode) {
        return rmb.selectServerInfo(serverCode);
    }
    //查询该用户的会员类型
    @Override
    public String meSelectByUsername(String userName) {
        String rs="";
        List<memberuser> mu=rmb.meSelectByUsername(userName);
          String mus=JSONObject.toJSONString(mu);
           List<Integer>lists=new ArrayList();
          JSONArray jp=JSONArray.parseArray(mus);
          System.out.println(mus+"mus");
          System.out.println(jp+"jp");
          for(int i=0;i<jp.size();i++){
              Integer s= (Integer) jp.getJSONObject(i).get("memberCode");
              System.out.println(s+"sss");
              lists.add(s);
          }
          if(mu.size()==0){
              rs="0";//说明不是会员
              return rs;
          }
          System.out.println(rs);
        Set<Integer> set = new HashSet<Integer>(lists);
        if(set.contains(1)&&set.contains(3)&&!set.contains(3)){
            rs="5";//说明是会员开通了金.铜牌VIP
        }else if(set.contains(1)&&set.contains(3)&&set.contains(2)){
            rs="7";//说明3个会员都开通了
        }else if(set.contains(3)){
            rs="1";//说明开通了铜牌VIP
        }else if(set.contains(2)){
            rs="2";//说明开通了银牌VIP
        }else if(set.contains(2)&&set.contains(3)&&!set.contains(1)){
            rs="3";//说明开通了银牌VIP
        }else if(set.contains(1)){
            rs="4";//说明开通了金牌VIP
        }else if(set.contains(1)&&set.contains(2)&&!set.contains(3)){
            rs="6";//说明是会员开通了金.银牌VIP
        }
        return rs;
    }
  //查询时间为了扣除会员次数  会员情况
    @Autowired
    filePart filePart;

    @Override
    public Result SelectBydata(String userName, Integer code,String imgName,String imgDetil,String imgResult,String imgType) {
        Result rs=new Result();
        List<String> list=rmb.SelectBydata(userName);
        System.out.println(list);
        int systemCount=0;
        int personCount=0;
        for(int i=0;i<list.size();i++){
          try{
              if(code==4){
                  //获取到了当前会员的所有开通时间，按照开通时间去查询扣去次数
                  System.out.println(i);
                  systemCount=systemCount(userName,list.get(i).toString());//通过当前时间获取系统次数
                  if(systemCount>0){//如果有值扣减次数  修改使用次数加一
                      updateSystem(userName,list.get(i).toString());
                      filePart.addImg(userName,imgName,  imgDetil,  imgResult, "系统鉴定",0,"会员服务",0, imgCard.Show()); //添加图片记录

                      rs.setCode(1);
                      rs.setResult(filePart.idShow(imgName));
                      return rs;
                  }else {
                      rs.setCode(0);

                  }
              }
          }catch (Exception e){
            throw  e;
          }
            try{
                if(code==5){
                    //获取到了当前会员的所有开通时间，按照开通时间去查询扣去次数
                    System.out.println(list.get(i).toString());
                    personCount=personCount(userName, list.get(i).toString());//通过当前时间获取人工次数
                    if(personCount>0){//如果有值扣减次数  修改使用次数加一
                        updatePerson(userName,list.get(i).toString());
                        filePart.addImg(userName,imgName,  imgDetil,  "正在鉴定", "专家鉴定",0,"会员服务",0,imgCard.Show()); //添加图片记录
                        rs.setCode(1);
                        rs.setResult(filePart.idShow(imgName));
                        return rs;
                    } else {
                        rs.setCode(0);
             }
       }
            }catch (Exception e){
                throw e;
                //return  rs;
            }
    }
        return rs;
    }
    //查询该用户是否只开通了铜牌
    @Override
    public Result SelectBydataTong(String userName) {//第一不是会员 第二仅是铜牌会员 第三呢金银中有专家鉴定的次数  第四种专家鉴定次数用完
        Result rs=new Result();
        //查询是否是会员
        memberuser us = rmb.us(userName);
        if(us==null){
            rs.setCode(0);//不是会员返回0
        }else {
            List<memberuser> mu = rmb.meSelectByUsername(userName);
            String mus = JSONObject.toJSONString(mu);
            List<Integer> lists = new ArrayList();
            JSONArray jp = JSONArray.parseArray(mus);
            System.out.println(mus + "mus");
            System.out.println(jp + "jp");
            for (int i = 0; i < jp.size(); i++) {
                Integer s = (Integer) jp.getJSONObject(i).get("memberCode");
                System.out.println(s + "sss");
                lists.add(s);
            }
            System.out.println(rs);
            Set<Integer> set = new HashSet<Integer>(lists);
            if (set.contains(3)&&!set.contains(2)&&!set.contains(1)) {
                rs.setCode(1);//说明开通了铜牌VIP
            } else {
                //查询其他会员的专家次数
                memberuser mn=rmb.meSleectPerson(userName);
                if(mn.getPersonCount()>0){
                    rs.setCode(2);//说明有次数
                }else {
                    rs.setCode(3);//说明没有次数
                }
            }
        }
        return rs;
    }

    //通过时间获取系统鉴定次数
    @Override
    public int systemCount(String userName, String data) {
        return rmb.systemCount(userName,data);
    }
    //通过时间获取人工鉴定次数
    @Override
    public int personCount(String userName, String data) {
        return rmb.personCount(userName,data);
    }
     //修改系统使用次数
    @Override
    public int updateSystem(String userName, String data) {
        return rmb.updateSystem(userName,data);
    }
    //修改人工使用次数
    @Override
    public int updatePerson(String userName, String data) {
        return rmb.updatePerson(userName,data);
    }
   //添加图片记录
   @Autowired
   filePart fileParts;
    @Override
    public Result addImg(String userName,String imgName,String imgDetil,String imgResult,String imgType,Integer payMoney,String payType) {
        Result rs=new Result();
          int i=fileParts.addImg(userName,imgName,imgDetil,imgResult,imgType,payMoney,payType,0,imgCard.Show());
   if(i>0){
       rs.setCode(1);
   }else {
       rs.setCode(0);
   }
        return rs;
    }
   //添加司法信息
   @Autowired
   filePart fileParta;
    @Override
    public int addElisor(String name, String phone, String userName, String email, String context, Integer imgId) {
        //添加图片记录
        int i=fileParta.addImg(userName,null,null,"正在受理","司法鉴定",0,"未支付",imgId,imgCard.Show());
        return rmb.addElisor(name,phone,userName,email,context,imgId);
    }
   //添加商品ID信息表  为了查询该商品支付是否成功
    @Override
    public int addGoods(String goodsId, String result) {
        return rmb.addGoods(goodsId,result);
    }
   //修改商品id的结果
    @Override
    public int upGoods(String result, String goodsId) {
        return rmb.upGoods(result,goodsId);
    }
    //查询该商品id的结果得到支付情况
    @Override
    public String selectResult(String goodsId) {
        return rmb.selectResult(goodsId);
    }
    //查询该用户的最后一张的图片地址，鉴定结果，时间,是否有鉴定记录
    @Override
    public Result SelectByifo(String userName) {
        imgInfo ifo=rmb.SelectByifo(userName);
        if(ifo==null){
            rs.setCode(0);//说明该用户没有图鉴记录
        }else{
            rs.setResult(ifo.getImgResult());//鉴定结果
            rs.setMsg(ifo.getImgDetil());//图片访问地址
            Map<String,String> map =new HashMap<>();
            map.put("id",ifo.getImgCard());
            map.put("imgAdddress",ifo.getImgAddress());
            rs.setMap(map);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            long dateOne=new Date().getTime();//当前时间
            long dateTwo=ifo.getDate().getTime();//插入数据库中的时间
            int c = (int)((dateOne - dateTwo) / 1000);
            rs.setId(c);//相差的秒数
            rs.setCode(1);//说明该用户有图鉴记录
        }
        return rs;
    }
    //查看该用户鉴定的图片存不存在
    @Override
    public imgInfo SelectimgInfo(String userName,String imgName) {
        return rmb.SelectimgInfo(userName,imgName);
    }
    //修改当前用户鉴定的图片记录
    @Override
    public int updateImgInfo(String imgDetil, String imgResult, double payMoney, String payType, String userName, String imgName) {
        return rmb.updateImgInfo(imgDetil,imgResult,payMoney,payType,userName,imgName);
    }
    //查询司法表
    @Override
    public elisor selectElisor(String userName,Integer imgId) {
        return rmb.selectElisor(userName,imgId);
    }

}
